/*!
* Register Verify
* Version: 1.0.0
* Modify: By Nian.Liu
* Date: 08-22-2013 18:11:48 (GMT Time)
*/
//js去除空格函数
function LTrim(str) {
    var i;
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) != " " && str.charAt(i) != " ") break;
    }
    str = str.substring(i, str.length);
    return str;
}
function RTrim(str) {
    var i;
    for (i = str.length - 1; i >= 0; i--) {
        if (str.charAt(i) != " " && str.charAt(i) != " ") break;
    }
    str = str.substring(0, i + 1);
    return str;
}
function Trim(str) {
    return LTrim(RTrim(str));
}

function getLength(str) {
    return str.replace(/([^\x00-\xFF])/g, '**').length; ;
}
//检测用户名输入是否以数字开头
function checkNameBeginByNum(str) {
    var number = str.substring(0, 1);
    var reg = /^\d+$/;
    if (!number.match(reg)) {
        return true;
    } else {
        return false;
    }
}
// 判断昵称是否重名
function ajaxNkname(str) {
    $.ajaxSetup({
        async: false
    });
    var url = "/Account/Ajax_IsExist";
    //var nkName = tagEle.signName.val();
    var nickname = encodeURI(str);
    var rs = null;
    jQuery.post("/Account/Ajax_IsExist", {
        "name": nickname
    }, function (data) {
        rs = data;
    }, "json");
    return rs;
}
//检测用户名信息
var rsName = false;
var bName = "";
function chekUsNamefor() {
    var nkName = tagEle.signName.val();
    var myReg = /^[_A-Za-z0-9\u4e00-\u9fa5]+$/;
    if (bName != nkName) {
        rsName = false;
    }
    if (rsName) {
        return true;
    }
    bName = nkName;
    tagEleTip.signNameTip.attr("style", "");
    if (nkName == "" || nkName == null || nkName == '请输入字母、数字或中文') {
        tagEleTip.signNameTip.html("用户名不能为空");
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    } else if (!checkNameBeginByNum(nkName)) {
        tagEleTip.signNameTip.html("不能数字开头");
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    } else if (getLength(nkName) < 4) {
        tagEleTip.signNameTip.html("昵称太短了");
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    } else if (getLength(nkName) > 18) {
        tagEleTip.signNameTip.html("昵称过长");
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    } else if (!myReg.test(nkName)) {
        tagEleTip.signNameTip.html("昵称只能为字母、数字或中文");
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    } else {
        tagEleTip.signNameTip.html("");
    }
    var rs = ajaxNkname(nkName);
    if (rs.Status == 1) {
        tagEleTip.signNameTip.html("该用户" + rs.Info);
        tagEleTip.signNameTip.css("color", "#FF0000");
        return false;
    }
    else {
        tagEleTip.signNameTip.html("");
        tagEleTip.signNameTip.append("<i class='icons green-proper'></i>");
    }
    rsName = true;
    return true;
}

function chekPNamefor() {
    var nkName = tagEle.signPName.val();
    var rs = ajaxNkname(nkName);
    if (rs.Status == 0) {
        tagEleTip.signPNameTip.html("该用户名不存在");
        tagEleTip.signPNameTip.css(
        {
            display: "inline-block",
            position: "absolute",
            top: 70,
            right: 615,
            color: "#ff0000"
        });
        return false;
    } else {
        tagEleTip.signPNameTip.html("");
        tagEleTip.signPNameTip.append("<i class='icons green-proper'></i>");
    }
    return true;
}

//密码输入提示
tagEle.signPwd.focus(function () {
    if ($(this).val() == '') {
        tagEleTip.signPwdTip.css({
            display: "inline-block",
            position: "absolute",
            top: 150,
            left: 365

        });

        tagEleTip.signPwdTip.css({ textalign: "center" });
        tagEleTip.signPwdTip.html("6-16个字符，建议使用字母<br>加数字或符号的组合密码");
        tagEleTip.signPwdTip.css("color", "#8a949c");
    }
})
//验证密码
function chekPwdfor() {
    var re = false;
    var pwd = tagEle.signPwd.val();
    tagEleTip.signPwdTip.attr("style", "");
    if (pwd == "" || pwd == null) {
        tagEleTip.signPwdTip.html("密码不能为空");
        tagEleTip.signPwdTip.css("color", "#FF0000");
        return re;
    } else if (pwd.length < 6 || pwd.length > 16) {
        tagEleTip.signPwdTip.html("长度:6-16位");
        tagEleTip.signPwdTip.css("color", "#FF0000");
        return re;
    }
    else {
        tagEleTip.signPwdTip.html("");
        tagEleTip.signPwdTip.append("<i class='icons green-proper'></i>");
        return true;
    }
}
//验证重新输入的密码
function checkPWDui() {  //密码确认
    var re = false;
    var PWDui = tagEle.signPwdRepeat.val();
    var PWD = tagEle.signPwd.val();
    if ($.trim(PWD) == "" || PWD == null) {
        tagEleTip.signPwdRepeatTip.html("请先输入密码");
        tagEleTip.signPwdRepeatTip.css("color", "#FF0000");
        return re;
    } else if (PWDui == "" || PWDui == null) {
        tagEleTip.signPwdRepeatTip.html("请再次输入");
        tagEleTip.signPwdRepeatTip.css("color", "#FF0000");
        return re;
    } else if (PWD != PWDui) {
        tagEleTip.signPwdRepeatTip.html("密码不一致");
        tagEleTip.signPwdRepeatTip.css("color", "#FF0000");
        return re;
    } else if (PWD == PWDui) {
        tagEleTip.signPwdRepeatTip.html("");
        tagEleTip.signPwdRepeatTip.append("<i class='icons green-proper'></i>");
        re = true;
        return re;
    }
}
//验证码校验
function chekVerifyCode() { // 验证码输入提示
    var vCode = tagEle.authCode.val();
    if (vCode == "" || vCode == null) {
        tagEleTip.signAuthCodeTip.html("请输入验证码");
        tagEleTip.signAuthCodeTip.css("color", "#FF0000");
        return false;
    } else {
        tagEleTip.signAuthCodeTip.html("");
    }
    return true;
}
// 刷新验证码
function coderefresh() {
    var id = document.getElementById("imgverCode");
    var d = new Date();
    id.src = environment.basePath + '/secure/verifyCode.jsp?a=' + d.getTime();
}
// 下一步按钮提交操作
function register() {
    $('#signSubmit').attr('href', 'javascript:;');
    var agreement = $("#protocol");
    var t1 = chekUsNamefor();
    if (!t1) {
        $('#signSubmit').attr('href', 'javascript:register();');
        return;
    }
    var t2 = chekPwdfor();
    if (!t2) {
        $('#signSubmit').attr('href', 'javascript:register();');
        return;
    }
    var t3 = checkPWDui();
    if (!t3) {
        $('#signSubmit').attr('href', 'javascript:register();');
        return;
    }
    var t4 = chekVerifyCode();
    if (!t4) {
        $('#signSubmit').attr('href', 'javascript:register();');
        return;
    }

    if (typeof (agreement.attr("checked")) == 'undefined') {
        tagEleTip.protocolTip.html("请同意协议");
        tagEleTip.protocolTip.css("color", "#FF0000");
        $('#signSubmit').attr('href', 'javascript:register();');
        return;
    } else {
        tagEleTip.protocolTip.html("");
    }

    var url = environment.basePath + "/secure/newRegisterUser.action";
    var nickname = tagEle.signName.val();
    var password = tagEle.signPwd.val();
    var verifycode = tagEle.authCode.val();
    var rs = null;
    $(".reg-items :input").attr("disabled", true);
    $.post(url, {
        "nickname": nickname,
        "password": password,
        "roles": 1,
        "verifycode": verifycode
    }, function (data, varStatus) {
        if (data == "0") {
            $(".reg-items :input").removeAttr('disabled');
            tagEleTip.signAuthCodeTip.html("用户名、密码或验证码错误");
            tagEleTip.signAuthCodeTip.css("color", "#FF0000");
            coderefresh();
            $('#signSubmit').attr('href', 'javascript:register();');
            return;
        } else if (data == "-2") {
            $(".reg-items :input").removeAttr('disabled');
            tagEleTip.signNameTip.html("ID已被注册");
            tagEleTip.signNameTip.css("color", "#FF0000");
            coderefresh();
            $('#signSubmit').attr('href', 'javascript:register();');
            return;
        } else if (data > 0) {
            var callbackUrl = environment.basePath + "/secure/register/verify/";
            window.location.href = decodeURIComponent(callbackUrl);
        }
    }, "text");
}